<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="疾病名称">
          <a-input placeholder="请输入疾病名称" v-decorator="['name', {rules: [{required: true,message: '请输入疾病名称!',}]}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="主疾病">
          <a-select show-search
                    option-filter-prop="children"
                    :filter-option="filterOption"
                    v-decorator="['diseaseId', {rules: [{required: true,message: '请选择主疾病!',}]}]" placeholder="主疾病">
            <a-select-option v-for="item in diseaseInfoList" :value="item.id" :key="item.id">
              {{ item.diseaseName }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="关联科室" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            mode="multiple"
            v-model="departmentIdList"
            style="width: 100%"
            placeholder="关联科室"
            @change="handleChange2" show-search option-filter-prop="children" :filter-option="filterOption2">
            <a-select-option v-for="item in departmentList" :value="item.id" :key="item.id">
              {{ item.departmentName }}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item label="儿科科室" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            mode="multiple"
            v-model="pediatricsIdList"
            style="width: 100%"
            placeholder="儿科科室"
            @change="handleChange4" show-search option-filter-prop="children" :filter-option="filterOption4">
            <a-select-option v-for="item in departmentList" :value="item.id" :key="item.id">
              {{ item.departmentName }}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item label="关联症状" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            mode="multiple"
            v-model="symptomIdList"
            style="width: 100%"
            placeholder="关联症状"
            @change="handleChange" show-search option-filter-prop="children" :filter-option="filterOption">
            <a-select-option v-for="item in symptomList" :value="item.id" :key="item.id">
              {{ item.symptomName }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="检查项目" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            mode="multiple"
            v-model="inspectionProjectIdList"
            style="width: 100%"
            placeholder="检查项目"
            @change="handleChange3" show-search option-filter-prop="children" :filter-option="filterOption3">
            <a-select-option v-for="item in inspectionProjectList" :value="item.id" :key="item.id">
              {{ item.projectName }}
            </a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import {getAction, httpAction} from '@/api/manage'
  import pick from 'lodash.pick'

  export default {
    name: "MedicalDetailsDiseaseInfoModal",
    data() {
      return {
        title: "操作",
        visible: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {},
        url: {
          add: "/hospital/medicalDetailsDiseaseInfo/add",
          edit: "/hospital/medicalDetailsDiseaseInfo/edit",
          getDiseaseInfoList: "/hospital/medicalDiseaseInfo/getListByDepartmentIdV2",
          getSymptomList: "/sms/smsBodyPartsSymptom/getList",
          getDepartmentList: "/hospital/medicalDepartmentInfo/getNodeDepartment",
          getInspectionProjectList: "/inspection/medicalInspectionProject/getProjectList",
        },
        diseaseInfoList: [],
        symptomList: [],
        symptomIdList: [],
        departmentList: [],
        departmentIdList: [],
        inspectionProjectIdList: [],
        inspectionProjectList: [],
        pediatricsIdList: [],
      }
    },
    created() {
    },
    mounted() {
      this.initIndex();
    },
    methods: {
      initIndex() {
        getAction(this.url.getDiseaseInfoList).then((res) => {
          if (res.success) {
            this.diseaseInfoList = res.result;
          }
        });
        getAction(this.url.getSymptomList).then((res) => {
          if (res.success) {
            this.symptomList = res.result;
          }
        });
        getAction(this.url.getDepartmentList).then((res) => {
          if (res.success) {
            this.departmentList = res.result;
          }
        });
        getAction(this.url.getInspectionProjectList).then((res) => {
          if (res.success) {
            this.inspectionProjectList = res.result;
          }
        });
      },
      add() {
        this.edit({});
      },
      edit(record) {
        this.symptomIdList = [];
        this.departmentIdList = [];
        this.inspectionProjectIdList = [];
        this.pediatricsIdList = [];
        this.form.resetFields();
        this.model = Object.assign({}, record);
        if (record.id) {
          this.symptomIdList = record.symptomIdList;
          this.departmentIdList = record.departmentIdList;
          this.inspectionProjectIdList = record.inspectionProjectIdList;
          this.pediatricsIdList = record.pediatricsIdList;
        }
        console.info("symptomIdList", this.symptomIdList);
        console.info("departmentIdList", this.departmentIdList);
        console.info("inspectionProjectIdList", this.inspectionProjectIdList);
        console.info("pediatricsIdList", this.pediatricsIdList);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'diseaseId', 'name', 'delFlag', 'symptomIdList'))
          //时间格式化
        });

      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            formData.symptomIdList = this.symptomIdList;
            formData.departmentIdList = this.departmentIdList;
            formData.inspectionProjectIdList = this.inspectionProjectIdList;
            formData.pediatricsIdList = this.pediatricsIdList;
            //时间格式化
            console.log(formData);
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },
      handleChange(value) {
        this.symptomIdList = value;
        console.info("symptomIdList", this.symptomIdList);
      },
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      handleChange2(value) {
        this.departmentIdList = value;
        console.info("departmentList", this.departmentIdList);
      },
      filterOption2(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      handleChange3(value) {
        this.inspectionProjectIdList = value;
        console.info("inspectionProjectIdList", this.inspectionProjectIdList);
      },
      filterOption3(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      handleChange4(value) {
        this.pediatricsIdList = value;
        console.info("pediatricsIdList", this.pediatricsIdList);
      },
      filterOption4(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
    }
  }
</script>

<style lang="less" scoped>

</style>